<?php

namespace Home\Controller;

use Think\Controller;

class QiyeController extends Controller
{
    /*
     * 平台服务Api
     */

    //获取接口地址
    private function getBaseUrl()
    {
         // return "http://112.33.1.160:81/healthcare_service_restful/";            //本地服务器
        return "http://service.health.10086.cn:8083/healthcare_service_restful/";     //线上服务器
    }

    private function getOperationsBaseUrl()
    {
        // return "http://112.33.1.160:81/healthcare_operations_restful/";     //本地开发
       return "http://health.10086.cn/healthcare_operations_restful/";     //正式服
    }

    private function appKey()
    {
        return 'ZSTJ';
    }

    private function invokeKey()
    {
//        return '[ZSTJ-S]240sgSOKlC';
        return '[ZSTJ-P]Q0hitfewWu';        //获取商城套餐列表
    }

    /*获取企业为员工定制的套餐*/
    public function getPackage(){
          //验证是否是企业用户
        $uid = empty($_GET["uid"])?cookie("uid"):$_GET["uid"];
        $uid=htmlspecialchars($uid);

        $org2015 = C("ORG_CODE_ZHENGQI");  // 2015 团体套餐
        $code2015=$this->getPackageZq($uid,$org2015);


        $org2016 = C("ORG_CODE_ZHENGQI_2016");  // 2016 团体套餐
        $code2016=$this->getPackageZq($uid,$org2016);



        if($code2015 == true){
            // $this->assign("qiye_show",2015);
            $qiye_show['qy2015']=2015;
        }
         if($code2016 == true){
            $qiye_show['qy2016']=2016;
        }
        if(!$code2015 && !$code2016){
            $qiye_show['status'] = 404;
        }

        $this->assign("qiye_show",$qiye_show);
        // $url_verifyUserOrg = $this->getBaseUrl() . "s/userService/verifyUserOrg";
        // $data = array(
        //     'appKey' => $this->appKey(),
        //     'invokeKey' => $this->invokeKey(),
        //     'loginName' =>$uid,
        //     'orgCode'=>C("ORG_CODE_ZHENGQI"),
        //     );
        // // var_dump($data);exit();
        // $ch = curl_init();
        // curl_setopt($ch, CURLOPT_URL, $url_verifyUserOrg);
        // curl_setopt($ch, CURLOPT_POST, 1);
        // curl_setopt($ch, CURLOPT_HEADER, 0);
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        // $return_verifyUserOrg = curl_exec($ch);
        // curl_close($ch);
        // $getinfo_verifyUserOrg = get_object_vars(json_decode($return_verifyUserOrg));
        // $result_verifyUserOrg = get_object_vars(json_decode($getinfo_verifyUserOrg['jsonResult']));

        // $dataMap=$result_verifyUserOrg['dataMap'];
        // $verifyResult=$dataMap->verifyResult;
        // // var_dump($verifyResult);exit();
        // if($verifyResult==="true"){
        //     $this->assign("qiye_show",true);
        // }else{
        //     $this->assign("qiye_show",false);
        // }
        $this->display();
    }
    /**
     * 获取政企用户组
     */
    private function getPackageZq($uid='',$orgCode=''){
        if(empty($uid) || empty($orgCode)) return false;
        $url_verifyUserOrg = $this->getBaseUrl() . "s/userService/verifyUserOrg";
        //dump($url_verifyUserOrg);exit;
        $data = array(
            'appKey' => $this->appKey(),
            'invokeKey' => $this->invokeKey(),
            'loginName' =>$uid,
            'orgCode'=>$orgCode,
            );
        // var_dump($data);exit();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url_verifyUserOrg);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return_verifyUserOrg = curl_exec($ch);
        curl_close($ch);
        $getinfo_verifyUserOrg = get_object_vars(json_decode($return_verifyUserOrg));
        $result_verifyUserOrg = get_object_vars(json_decode($getinfo_verifyUserOrg['jsonResult']));

        $dataMap=$result_verifyUserOrg['dataMap'];
        $verifyResult=$dataMap->verifyResult;
        // dump($verifyResult);
        if($verifyResult==="true"){
            return true;
        }else{
            return false;
        }
    }
    /*选择套餐2015年流程*/
    public function selectPackage(){
        //获取用户信息
        $uid = empty($_GET["uid"])?cookie("uid"):$_GET["uid"];
        $uid=htmlspecialchars($uid);
        // var_dump($uid);exit();
        $Appointment = M("appointment");
        if (!empty($uid) && empty($_POST)) {
            // echo "dfd";exit();
             $condition['uid']=$uid;
             $condition['type']="2";      //属于团体预约订单
             $condition['package_year']="2015";      //属于团体预约订单2015年订单
             // $condition['add_date']=array("between","2015-12-15 00:00:00,2016-12-31 23:59:59");
             $people =$Appointment->where($condition)->find();
             // var_dump($Appointment->getLastSql());exit();
             // echo "fdfd";exit();
             // dump($people);exit();
             if(!is_null($people)){

               //获取体检套餐详情
                $url = $this->getBaseUrl() . "s/infoPhysicalExamination/getInfoPhysicalExaminationList";
                $data = array(
                    'appKey' => $this->appKey(),
                    'invokeKey' => $this->invokeKey(),
                    'packageId'=>$people['item_id'],
                    'status' => "",         //0表示上架。但是套餐可能是下架，所以status不适合设置成0
                    );
                // var_dump($data);exit();
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                $return = curl_exec($ch);
                curl_close($ch);

                $getinfo = get_object_vars(json_decode($return));
                $result = get_object_vars(json_decode($getinfo['jsonResult']));
                $dataList = (array)$result['dataList'];
                // var_dump($dataList);exit();
                $dataInfo=$dataList[0];
                // var_dump($dataInfo);exit();

                $people['packageName']=$dataInfo->name;
                $people['examinationItems']=$dataInfo->examinationItems;

                $identity_no_length=strlen($people['u_card']);

                //从身份证判定性别
                if($identity_no_length==18){
                    $birth=substr($people['u_card'], 6,4);
                    $gender_num=substr($people['u_card'],16,1 );
                }elseif($identity_no_length==15){
                    $birth=substr($people['u_card'], 6,2);
                    $birth="19".$birth;
                    $gender_num=substr($people['u_card'], 14,1);
                }
                if($gender_num%2==0 || $gender_num%2==2){
                    $people['gender']="女";
                }else{
                    $people['gender']="男";
                }

                $people['age']=date("Y")-$birth;
                $this->assign("people",$people);
                $this->display("viewPackage");
                exit();

            }
            // echo "hello";exit();

            $url = "http://localhost" . U("Serviceapi/getUserByUid");     //获取用户uid
            $data = array(
                'appKey' => $this->appKey(),
                'invokeKey' => $this->invokeKey(),
                'uid' => $uid,
                );
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            $result = curl_exec($ch);
            // var_dump($result);exit();
            $data = json_decode($result);
            $jsonResult=$data->jsonResult;
            $jsonResult=json_decode($jsonResult);
            $data=$jsonResult->data;
            // var_dump($data);exit();
            if(!is_null($data)){
            $userInfo['uid']=$data->uid;
            $userInfo['gender']=$data->userGender;
            $userInfo['realname']=$data->userName;                      //姓名
            $userInfo['telephone']=$data->userPreferredMobile;  //电话号码
            $userInfo['identity_no']=$data->userIdCardNumber;       //身份证号


            $identity_no_length=strlen($userInfo['identity_no']);
            if($identity_no_length==18){
                $birth=substr($userInfo['identity_no'], 6,4);
                $gender_num=substr($userInfo['identity_no'], 16,1);
            }elseif($identity_no_length==15){
                $birth=substr($userInfo['identity_no'], 6,2);
                $birth="19".$birth;
                $gender_num=substr($userInfo['identity_no'], 14,1);
            }
            if($gender_num%2==0 || $gender_num%2==2){
                $userInfo['gender']="女";
            }else{
                $userInfo['gender']="男";
            }
            $userInfo['age']=2015-$birth;

            //存入cookie，供viewPackage页面使用
            cookie("gender", $userInfo['gender']);
            cookie("age", $userInfo['age']);

            //根据性别和年龄确定适用的体检套餐

            if($userInfo["gender"]=="男" && $userInfo['age']<40){
                $packageList=array(150095=>"",150089=>"");
            }elseif ($userInfo['gender']=="男" && $userInfo['age']>=40) {
                $packageList=array(150099=>"",150092=>"");
            }elseif($userInfo["gender"]=="女" && $userInfo['age']<40){
                $packageList=array(150098=>"",150096=>"",150091=>"",150090=>"");
            }elseif($userInfo['gender']=="女" && $userInfo['age']>=40){
                $packageList=array(150094=>"",150093=>"",150101=>"",150100=>"");
            }
            }
           //检查缓存是否存在
            // $cachePackageList=S("cachePackageList","",array("prefix"=>"cachePackageList_".$userInfo['uid'],"temp"=>RUNTIME_PATH.'Temp/package'));
            // $cachePackageDetailList=S("cachePackageDetailList","",array("prefix"=>"packageDetailList_","temp"=>RUNTIME_PATH.'Temp/package'));

            // var_dump($cachePackageDetailList);exit();
            // var_dump($cachePackageList);exit();

                // echo "hello";exit();
                   //获取体检套餐详情
                    $url = $this->getBaseUrl() . "s/infoPhysicalExamination/getInfoPhysicalExaminationList";
                    $data = array(
                        'appKey' => $this->appKey(),
                        'invokeKey' => $this->invokeKey(),
                        'pageSize'=>"200"
                        // 'status' => 1,
                        );
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                    $return = curl_exec($ch);
                    curl_close($ch);

                    $getinfo = get_object_vars(json_decode($return));
                    $result = get_object_vars(json_decode($getinfo['jsonResult']));
                    $dataList = (array)$result['dataList'];
                    // var_dump($dataList);exit();

                    $arr=array();
                    foreach ($dataList as $key => $value) {
                        if(array_key_exists($value->packageId, $packageList)){
                            $packageList[$value->packageId]=$value->name;
                        }
                    }
                    // $cachePackageList=S("cachePackageList",$packageList,array("prefix"=>"cachePackageList_".$userInfo['uid'],"temp"=>RUNTIME_PATH.'Temp/package'));
                    // $cachePackageDetailList=S("cachePackageDetailList",$dataList,array("prefix"=>"packageDetailList_","temp"=>RUNTIME_PATH.'Temp/package'));

                    if($packageList[150095]){unset($packageList[150095]);}
                    if($packageList[150099]){unset($packageList[150099]);}
                    if($packageList[150098]){unset($packageList[150098]);}
                    if($packageList[150096]){unset($packageList[150096]);}
                    if($packageList[150101]){unset($packageList[150101]);}
                    if($packageList[150100]){unset($packageList[150100]);}

            // var_dump($packageList);exit();
            $this->assign("userInfo",$userInfo);
            $this->assign("packageList", $packageList);
            $this->assign("dataList", $dataList);
            $this->display();
        }

        //保存数据到数据库，生成订单
        if(!empty($_POST)){
            //生成订单，同步订单到西门子
            $purchaeData["uid"]=$uid;
            $purchaeData['item_id']=$_POST['item_id'];
            $purchaeData['item_count']=1;
            $purchaeData['total_price']=$_POST['sale_price']*1;
            $purchaeData['add_date']=date("Y-m-d h:i:s");
            $purchaeData['update_date']=date("Y-m-d h:i:s");
            $purchaeData['pay_status']=1;

            // var_dump($purchaeData);exit();
            $sn_end = explode(" ",microtime());
            $sn_end=$sn_end[1];

            $purchaeData['sn']=date("Ymd").$sn_end;
            $Purchase = M("purchase");
            $ret = $Purchase->data($purchaeData)->add();
            // var_dump($Purchase->getLastSql());exit();
            // var_dump($ret);exit;
             //同步用户订单数据数据到平台
            $url = "http://localhost" . U("H5api/purchaseInfoSync");
            // var_dump($url);exit();
            $data=array(
                'out_trade_no'=>  $purchaeData['sn'],
                );
            // var_dump($data);exit();
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            // var_dump($ch);exit;
            $return = curl_exec($ch);
            // var_dump($return);exit;
            curl_close($ch);

            $getinfo = get_object_vars(json_decode($return));
            // var_dump($getinfo);exit();
            $jsonResult =get_object_vars(json_decode($getinfo['jsonResult']));
            $resultCode=$jsonResult['resultCode'];


            //生成预约订单
            $itemData['uid']=$uid;
            $itemData['item_id']=$_POST['item_id'];
            $itemData['u_name']=$_POST['realname'];
            $itemData['u_telephone']=$_POST['telephone'];
            $itemData['u_card']=$_POST['identity_no'];
            $itemData['appointment_status']=1;
            $itemData['add_date']=date("Y-m-d h:i:s");
            $itemData['update_date']=date("Y-m-d h:i:s");
            $itemData['type']=2;        //属于团体预约订单
            $itemData['package_year']=2015;        //属于2015年团体预约订单


            // 若网络出现问题避免重复订单
            $repeatinfo['uid']=$uid;
            $repeatinfo['type']="2";      //属于团体预约订单
            $repeatinfo['package_year']="2015";      //属于团体预约订单2015年订单
            $repeatJudge = $Appointment->where($repeatinfo)->find();

            if($repeatJudge){
                $jumpUrl='<script language="javascript">
                document.location="/yjxtj/h5.php?m=app&a=getPurchase&uid='.$uid.'";
                </script>';
                echo $jumpUrl;
            }else{
                $ret = $Appointment->add($itemData);
                if($ret){
                    $jumpUrl='<script language="javascript">
                    document.location="/yjxtj/h5.php?m=app&a=getPurchase&uid='.$uid.'";
                    </script>';
                    echo $jumpUrl;
                }else{
                    $this->error("保存失败");
                }
            }
                // $ret = $Appointment->add($itemData);
                // if($ret){
                //     $jumpUrl='<script language="javascript">
                //     document.location="/yjxtj/h5.php?m=app&a=getPurchase&uid='.$uid.'";
                //     </script>';
                //     echo $jumpUrl;
                // }else{
                //     $this->error("保存失败");
                // }
        }
    }

    /*选择套餐2016年套餐列表*/
    /**
     * 2016套餐列表
     */
    public function selectList(){
        $uid = empty($_GET["uid"])?cookie("uid"):$_GET["uid"];
        $uid=htmlspecialchars($uid);

        $org2016 = C("ORG_CODE_ZHENGQI_2016");  // 2016 团体套餐
        $code2016=$this->getPackageZq($uid,$org2016);
        $code2016 ? $this->assign("code_2016",1) : $this->assign("code_2016",2);

        if($code2016){
            // 获取用户信息

            // $url = "http://localhost" . U("Serviceapi/getUserByUid");  //获取用户基本信息
            // $data = array(
            //     'appKey' => $this->appKey(),
            //     'invokeKey' => $this->invokeKey(),
            //     'uid' => $uid,
            // );
            // $ch = curl_init();
            // curl_setopt($ch, CURLOPT_URL, $url);
            // curl_setopt($ch, CURLOPT_POST, 1);
            // curl_setopt($ch, CURLOPT_HEADER, 0);
            // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            // curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            // $result = curl_exec($ch);
            // $data = json_decode($result);
            // $jsonResult=$data->jsonResult;
            // $jsonResult=json_decode($jsonResult);
            // $data=$jsonResult->data;
            //     if(!is_null($data)){
            //         $info = '';
            //         $userInfo['gender']=$data->userGender; //性别
            //         $userInfo['identity_no']=$data->userIdCardNumber;       //身份证号
            //         // $userInfo['identity_no']='371482198806093551';       //身份证号 测试用
            //         $identity_no_length=strlen($userInfo['identity_no']);
            //         if($identity_no_length==18){
            //             $birth=substr($userInfo['identity_no'], 6,4);
            //             $gender_num=substr($userInfo['identity_no'], 16,1);
            //         }elseif($identity_no_length==15){
            //             $birth=substr($userInfo['identity_no'], 6,2);
            //             $birth="19".$birth;
            //             $gender_num=substr($userInfo['identity_no'], 14,1);
            //         }
            //         if($gender_num%2==0 || $gender_num%2==2){
            //             $userInfo['gender']="女";
            //         }else{
            //             $userInfo['gender']="男";
            //         }
            //         $years = date('Y',time());
            //         $userInfo['age'] = $years-$birth;
            //     }


            // // 好牙医传参
            // $info = $info .'name='. $data->userName; // 姓名
            // $info = $info .'&sex='. $userInfo['gender']; // 姓别
            // $info = $info .'&tel='. $data->userPreferredMobile; //电话
            // $info = $info .'&age='. $userInfo['age']; // 年龄
            // dump($info);


            $dataInfo = $this->getUserInfo($uid);
            $infoPraram = '';
            // 好牙医传参
            $infoPraram = $infoPraram .'name='. $dataInfo['userName']; // 姓名
            $infoPraram = $infoPraram .'&sex='. $dataInfo['gender']; // 姓别
            $infoPraram = $infoPraram .'&tel='. $dataInfo['userPreferredMobile']; //电话
            $infoPraram = $infoPraram .'&age='. $dataInfo['age']; // 年龄
            // dump($infoPraram);

            // dump($dataInfo);exit;


            $cryptkey = hash('sha256','__tazai_wolf__key',true);   // 加密key
            $iv = "1234567891234567";    // 秘钥
            $secretdata = $infoPraram;   // 加密内容
            $infoData = $this->encodeDes($cryptkey,$iv,$secretdata);
            $infoData = urlencode($infoData);
            // dump($infoData);exit;
            $this->assign('info',$infoData);
        }
        $this->display();
    }
    /**
     * des解密字符串
     */
    private function decodedDes($cryptkey, $iv, $secretdata){
        return openssl_decrypt($secretdata,'aes-256-cbc',$cryptkey,false,$iv);
    }
    /**
     * des加密字符串
     */
    private function encodeDes($cryptkey, $iv, $secretdata){
        return openssl_encrypt($secretdata,'aes-256-cbc',$cryptkey,false,$iv);
    }
    /**
     * [getUserInfo description] 获取用户基本信息好牙医传参
     * @return [type] [description]
     */
    private function getUserInfo($uid){
        // $uid = 'MU_lG1809920';
        if(!isset($uid)) return false;
        $url = "http://localhost" . U("Serviceapi/getUserByUid");  //获取用户基本信息
            $data = array(
                'appKey' => $this->appKey(),
                'invokeKey' => $this->invokeKey(),
                'uid' => $uid,
            );
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            $result = curl_exec($ch);
            $data = json_decode($result);
            $jsonResult=$data->jsonResult;
            $jsonResult=json_decode($jsonResult);
            $data=$jsonResult->data;
            // dump($data);
            $info['userName'] = $data->userName;
            $info['gender']=$data->userGender; //性别
            $info['identity_no']=$data->userIdCardNumber;       //身份证号
            $identity_no_length=strlen($info['identity_no']);
                if($identity_no_length==18){
                    $birth=substr($info['identity_no'], 6,4);
                    $gender_num=substr($info['identity_no'], 16,1);
                }elseif($identity_no_length==15){
                    $birth=substr($info['identity_no'], 6,2);
                    $birth="19".$birth;
                    $gender_num=substr($info['identity_no'], 14,1);
                }
                if($gender_num%2==0 || $gender_num%2==2){
                    $info['gender']="女";
                }else{
                    $info['gender']="男";
                }
            $years = date('Y',time());
            $info['age'] = $years-$birth;
            $info['userPreferredMobile'] = $data->userPreferredMobile;
            // dump($info);


            return $info;
    }
    /**
     *  选择2016体检套餐,
     *
     */
    public function choosePackage(){
        //获取用户信息
        $uid = empty($_GET["uid"])?cookie("uid"):$_GET["uid"];
        if(empty($uid)) return false; // Uid检测
        $uid=htmlspecialchars($uid);
        $org2016 = C("ORG_CODE_ZHENGQI_2016");  // 2016 团体套餐
        $code2016=$this->getPackageZq($uid,$org2016);
        //dump($code2016);
        //检测是否为政企员工
        if(!$code2016){ redirect("getPackage?uid={$uid}", 0, ""); }

        $Appointment = M("appointment");
        if (!empty($uid) && empty($_POST)) {
            $condition['uid']=$uid;
            $condition['type']="2";      //属于团体预约订单
            $condition['package_year']="2016";      //属于团体预约订单2016年订单
                // $condition['add_date']=array("between","2015-12-15 00:00:00,2016-12-31 23:59:59");
            $people =$Appointment->where($condition)->find();

            // 存在表示已经选择过团体套餐,直接解析模板
            if(!is_null($people)){
                // 获取体检套餐详情
                $url = $this->getBaseUrl() . "s/infoPhysicalExamination/getInfoPhysicalExaminationList";
                $data = array(
                        'appKey' => $this->appKey(),
                        'invokeKey' => $this->invokeKey(),
                        'packageId'=>$people['item_id'],
                        'status' => "",         //0表示上架。但是套餐可能是下架，所以status不适合设置成0
                    );
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                $return = curl_exec($ch);
                curl_close($ch);
                $getinfo = get_object_vars(json_decode($return));
                $result = get_object_vars(json_decode($getinfo['jsonResult']));
                $dataList = (array)$result['dataList'];
                $dataInfo=$dataList[0];
                // var_dump($dataInfo);exit();
                $people['packageName']=$dataInfo->name;
                $people['examinationItems']=$dataInfo->examinationItems;
                $this->assign("people",$people);
                $this->display("viewPackage");
                exit();
            }

            // 不存在表示未选择体检套餐,重新获取
            $url = "http://localhost" . U("Serviceapi/getUserByUid");  //获取用户基本信息
            $data = array(
                'appKey' => $this->appKey(),
                'invokeKey' => $this->invokeKey(),
                'uid' => $uid,
            );

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            $result = curl_exec($ch);
            //var_dump($result);exit();
            $data = json_decode($result);
            $jsonResult=$data->jsonResult;
             // echo $jsonResult; exit;
            $jsonResult=json_decode($jsonResult);
            // dump($jsonResult);exit;
            $data=$jsonResult->data;
            //var_dump($data);exit();

            if(!is_null($data)){
                // $userInfo['uid']=$data->uid;
                $userInfo['uid']=$data->userUid;

                $userInfo['gender']=$data->userGender;
                $userInfo['realname']=$data->userName;                      //姓名
                $userInfo['telephone']=$data->userPreferredMobile;  //电话号码
                $userInfo['identity_no']=$data->userIdCardNumber;       //身份证号
                // $userInfo['identity_no']='371482198806093551';       //身份证号 测试用
                // $userInfo['identity_no']='371482195202032651';       //身份证号 测试用
                // dump($userInfo['identity_no']);exit;
                $identity_no_length=strlen($userInfo['identity_no']);
                if($identity_no_length==18){
                    $birth=substr($userInfo['identity_no'], 6,4);
                    $gender_num=substr($userInfo['identity_no'], 16,1);
                }elseif($identity_no_length==15){
                    $birth=substr($userInfo['identity_no'], 6,2);
                    $birth="19".$birth;
                    $gender_num=substr($userInfo['identity_no'], 14,1);
                }
                if($gender_num%2==0 || $gender_num%2==2){
                    $userInfo['gender']="女";
                }else{
                    $userInfo['gender']="男";
                }
                //$years = date('Y',time());
                //$userInfo['age']=$years-$birth;
                $userInfo['age']= 2016 - $birth;
                //存入cookie，供viewPackage页面使用
                cookie("gender", $userInfo['gender']);
                cookie("age", $userInfo['age']);

                //根据性别和年龄确定适用的体检套餐
                if($userInfo["gender"]=="男" && $userInfo['age']<35){
                    $packageList=array(C('ID_PACKAGE_MR_A')=>''); // 男35以内

                }else if($userInfo['gender']=="男" && $userInfo['age']>=35 && $userInfo['age'] < 45){
                    $packageList=array(C('ID_PACKAGE_MR_B')=>'');  // 男35-44

                }else if($userInfo['gender']=="男" && $userInfo['age']>=45){
                    $packageList=array(C('ID_PACKAGE_MR_C')=>''); //男45以上

                }else if($userInfo["gender"]=="女" && $userInfo['age']<35){
                    $packageList=array(C('ID_PACKAGE_MS_A1')=>'',C('ID_PACKAGE_MS_A2')=>''); // 女35以内 A1未婚  A2已婚

                }else if($userInfo["gender"]=="女" && $userInfo['age']>=35 && $userInfo['age'] < 45){
                    $packageList=array(C('ID_PACKAGE_MS_B1')=>'',C('ID_PACKAGE_MS_B2')=>''); // 女35-44  B1未婚  B2已婚

                }else if($userInfo['gender']=="女" && $userInfo['age']>=45){
                    $packageList=array(C('ID_PACKAGE_MS_C1')=>'',C('ID_PACKAGE_MS_C2')=>''); // 女45以上 C1未婚  C2已婚
                }

                // dump($userInfo);
                // dump($packageList);exit;
            }


            // 获取体检套餐详情
            $url = $this->getBaseUrl() . "s/infoPhysicalExamination/getInfoPhysicalExaminationList";
            $data = array(
                'appKey' => $this->appKey(),
                'invokeKey' => $this->invokeKey(),
                'pageSize'=>'20',
                'name'=>'政企16', // 这里输入套餐名字筛选套餐
                // 'status' => 1,
            );
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            $return = curl_exec($ch);
            curl_close($ch);
            $getinfo = get_object_vars(json_decode($return));
            $result = get_object_vars(json_decode($getinfo['jsonResult']));
            $dataList = (array)$result['dataList'];



            // dump($dataList);exit();
            foreach ($dataList as $key => $value) {
                if(array_key_exists($value->packageId, $packageList)){
                    // $packageList[$value->packageId]=$value->name;
                    $packageList[$value->packageId] = str_replace('团检','',$value->name);
                }
            }


            // unset($dataList['examinationItems']);
            // dump($dataList);exit;
            // dump($packageList);exit();
            // dump($userInfo);exit();
            $this->assign("userInfo",$userInfo);
            $this->assign("packageList", $packageList);
            $this->assign("dataList", $dataList);
            $this->display();
            exit();
        }

        // // post 提交到数据库
        if(!empty($_POST)){
            // 生成购买订单
            $purchaeData["uid"]=$uid;
            $purchaeData['item_id']=$_POST['item_id'];
            $purchaeData['item_count']=1;
            $purchaeData['total_price']=$_POST['sale_price']*1;
            $purchaeData['add_date']=date("Y-m-d h:i:s");
            $purchaeData['update_date']=date("Y-m-d h:i:s");
            $purchaeData['pay_status']=1;
            // dump($purchaeData);exit();

            $sn_end = explode(" ",microtime());
            $sn_end=$sn_end[1];
            $purchaeData['sn']=date("Ymd").$sn_end;
            $Purchase = M("purchase");
            $ret = $Purchase->data($purchaeData)->add();

            /*订单同步代码start*/
            $url = "http://localhost" . U("H5api/purchaseInfoSync");
            // dump($url);exit();
            $data=array(
                'out_trade_no'=>  $purchaeData['sn'],
                );
            // var_dump($data);exit();
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            $return = curl_exec($ch);
            // echo $return;exit;
            curl_close($ch);

            $getinfo = get_object_vars(json_decode($return));
            // dump($getinfo);exit();
            $jsonResult =get_object_vars(json_decode($getinfo['jsonResult']));
            $resultCode=$jsonResult['resultCode'];
            // dump($jsonResult);exit;
            /*订单同步代码stop*/

            // 生成预约订单
            $itemData['uid']=$uid;
            $itemData['item_id']=$_POST['item_id'];
            $itemData['u_name']=$_POST['realname'];
            $itemData['u_telephone']=$_POST['telephone'];
            $itemData['u_card']=$_POST['identity_no'];
            $itemData['appointment_status']=1;
            $itemData['add_date']=date("Y-m-d h:i:s");
            $itemData['update_date']=date("Y-m-d h:i:s");
            $itemData['type']=2;        //属于团体预约订单
            $itemData['package_year']=2016;        //属于2015年团体预约订单


            // 若网络出现问题避免重复订单
            $repeatinfo['uid'] = $uid;
            $repeatinfo['type']="2";      //属于团体预约订单
            $repeatinfo['package_year']="2016";      //属于团体预约订单2016年订单
            $repeatJudge = $Appointment->where($repeatinfo)->find();
            if($repeatJudge){
                $jumpUrl='<script language="javascript">document.location="/yjxtj/h5.php?m=app&a=getPurchase&uid='.$uid.'";</script>'; //生产
                // $jumpUrl='<script language="javascript">document.location="/yjxtj/source/h5.php?m=app&a=getPurchase&uid='.$uid.'";</script>'; //本地
                echo $jumpUrl;
            }else{
                $ret = $Appointment->add($itemData);
                if($ret){
                    $jumpUrl='<script language="javascript">document.location="/yjxtj/h5.php?m=app&a=getPurchase&uid='.$uid.'";</script>'; //生产
                    echo $jumpUrl;
                }else{
                    $this->error("保存失败");
                }
            }

            // $ret = $Appointment->add($itemData);
            // if($ret){
            //     $jumpUrl='<script language="javascript">document.location="/yjxtj/h5.php?m=app&a=getPurchase&uid='.$uid.'";</script>'; //生产
            //     // $jumpUrl='<script language="javascript">document.location="/yjxtj/source/h5.php?m=app&a=getPurchase&uid='.$uid.'";</script>'; //本地
            //     echo $jumpUrl;
            // }else{
            //     $this->error("保存失败");
            // }

        }


    }




}
